package com.yunfei.api.notice;

import com.yunfei.api.message.contants.AppMessageInformationCenterContants;
import com.yunfei.api.message.contants.LoginRoleConstants;
import com.yunfei.api.message.param.information.findAllList.MessageInformationFindAllDataListParam;
import com.yunfei.api.message.param.information.update.MessageInformationUpdateParam;
import com.yunfei.api.message.responseBody.templage.initPageData.MessageTemplateInitPageData;
import com.yunfei.api.notice.param.MessageNoticeParam;
import com.yunfei.api.notice.param.UpdateMessageNoticeParam;
import com.yunfei.api.notice.result.MessageNoticeReturnBo;
import com.yunfei.bo.TPersonalUserBo;
import com.yunfei.bo.messageSendDtl.MessageSendDtlSimpleBo;
import com.yunfei.bo.result.ResultModel;
import com.yunfei.bo.tDictionary.TdictionarySimpleBo;
import com.yunfei.enums.ResultStatus;
import com.yunfei.fegin.RedisFeignService;
import com.yunfei.mapper.aiiidb.*;
import com.yunfei.ribbon.RedisServiceRibbon;
import com.yunfei.service.RedisService;
import com.yunfei.utils.TokenUtil;
import com.yunfei.utils.page.PageCommon;
import com.yunfei.utils.string.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description: app消息中心：通知
 * @Author: zhengyunfei
 * @Date: 2018/6/15
 * @time:11:26
 */
@SuppressWarnings("ALL")
@Api(value = AppMessageInformationCenterContants.MESSAGE_INFORMATION_CENTER_API_NAMESPACE,tags = AppMessageInformationCenterContants.MESSAGE_INFORMATION_CENTER_API_NAMESPACE )
@RestController
@RequestMapping("/api/messageCenter/notice")
public class AppNoticeMessageApiController {
    @Autowired
    private TPersonalUserMapper tPersonalUserMapper;
    @Autowired
    private RedisFeignService redisFeignService;
    @Autowired
    private RedisService redisService;
    @Autowired
    private RedisServiceRibbon redisServiceRibbon;
    @Autowired
    private MsgRuleConfigMapper msgRuleConfigMapper;
    @Autowired
    private MessageSendDtlMapper messageSendDtlMapper;
    @Autowired
    private MessageNoticeMapper messageNoticeMapper;
    @Autowired
    private TdictionaryMapper tdictionaryMapper;
    @Autowired
    private Environment env;
    /**
    * @Description: 页面初始化
    * @Author: zhengyunfei
    * @Date: 2019/2/25
    * @time:11:38
    */
    @GetMapping(value = "/get",produces = "application/json;charset=UTF-8")
    @ApiOperation(value = AppMessageInformationCenterContants.MESSAGE_CENTER_NOTICE_LIST,notes = AppMessageInformationCenterContants.AUTHOR_NOTE,position = 1,response = MessageNoticeReturnBo.class)
    @ResponseBody
    public Object getNoticeList(MessageNoticeParam param){
        ResultModel resultModel=new ResultModel();
        try{
           // String custNo=param.getCustNo();//客户号
            String loginRole=param.getLoginRole();//登录角色，01客户，02理财师
            String pageNow=param.getPageNow();//当前页面
            String mobile=param.getMobile();
            int page=1;
            if(!StringUtil.isNullOrEmpty(pageNow)){
                page=Integer.parseInt(pageNow);
            }
            String msgContext=param.getMsgRuleName();//搜索关键字，根据内容检索
            Map<String,Object> queryMap=new HashMap<>();
            String token=param.getToken();
            String custNo= TokenUtil.getInstance().getUserId(token);
            if(LoginRoleConstants.LOGIN_ROLE_01.equals(loginRole)){
                TPersonalUserBo tPersonalUserBo=new TPersonalUserBo();
                tPersonalUserBo.setMobile(mobile);
                custNo=tPersonalUserMapper.getUserId(tPersonalUserBo);
            }
            queryMap.put("custNo",custNo);
            queryMap.put("msgContext",msgContext);
            queryMap.put("pageNow",(page-1)*20+1);
            queryMap.put("pageSize",20*page);
            Map<String,Object> resultMap=new HashMap<>();
            //消息通知列表
            List<MessageNoticeReturnBo> noticeList=messageNoticeMapper.findAllList(queryMap);
            int noReadMessageCount=(Integer) messageNoticeMapper.getNoReadMessageCount(queryMap);
            resultMap.put("list",noticeList);
            resultMap.put("noReadMessageCount",noReadMessageCount);
            resultModel.setData(resultMap);
            resultModel.setCode(ResultStatus.SUCCESS.getCode());
            resultModel.setMessage(ResultStatus.SUCCESS.getMessage());
        }catch (Exception e){
            resultModel.setCode(ResultStatus.ERROR.getCode());
            resultModel.setMessage(ResultStatus.ERROR.getMessage());
        }
        return resultModel;
    }
    /**
    * @Description: 更新已读消息
    * @Author: zhengyunfei
    * @Date: 2019/3/26
    * @time:9:45
    */
    @GetMapping(value = "/update",produces = "application/json;charset=UTF-8")
    @ApiOperation(value = AppMessageInformationCenterContants.MESSAGE_CENTER_NOTICE_UPDATE,notes = AppMessageInformationCenterContants.AUTHOR_NOTE,position = 1,response = MessageNoticeReturnBo.class)
    @ResponseBody
    public Object update(UpdateMessageNoticeParam param){
        ResultModel resultModel=new ResultModel();
        try{
            String msgId=param.getMsgId();//消息ID
            Map<String,Object> queryMap=new HashMap<>();
            queryMap.put("msgId",msgId);
            //更新消息状态为已读
            messageNoticeMapper.update(queryMap);
            resultModel.setCode(ResultStatus.SUCCESS.getCode());
            resultModel.setMessage(ResultStatus.SUCCESS.getMessage());
        }catch (Exception e){
            resultModel.setCode(ResultStatus.ERROR.getCode());
            resultModel.setMessage(ResultStatus.ERROR.getMessage());
        }
        return resultModel;
    }
    /**
     * @Description: 删除消息
     * @Author: zhengyunfei
     * @Date: 2019/3/26
     * @time:9:45
     */
    @GetMapping(value = "/delete",produces = "application/json;charset=UTF-8")
    @ApiOperation(value = AppMessageInformationCenterContants.MESSAGE_CENTER_NOTICE_DELETE,notes = AppMessageInformationCenterContants.AUTHOR_NOTE,position = 1,response = MessageNoticeReturnBo.class)
    @ResponseBody
    public Object delete(UpdateMessageNoticeParam param){
        ResultModel resultModel=new ResultModel();
        try{
            String msgId=param.getMsgId();//消息ID
            Map<String,Object> queryMap=new HashMap<>();
            queryMap.put("msgId",msgId);
            //删除消息
            messageNoticeMapper.delete(queryMap);
            resultModel.setCode(ResultStatus.SUCCESS.getCode());
            resultModel.setMessage(ResultStatus.SUCCESS.getMessage());
        }catch (Exception e){
            resultModel.setCode(ResultStatus.ERROR.getCode());
            resultModel.setMessage(ResultStatus.ERROR.getMessage());
        }
        return resultModel;
    }
}
